﻿using System;
using WholesaleOnline.Contract;
using WholesaleOnline.Controller.Request;
using WholesaleOnline.Controller.Routing;
using WholesaleOnline.Controller.Storage;
using WholesaleOnline.DataContract.Messaging;

namespace WholesaleOnline.Controller.Handlers
{
    public class AddToBasketHanlder : RequestHandler
    {
        private IBasketService m_basketService = null;

        public AddToBasketHanlder(IHttpStorage storage, IBasketService basketService)
            : base(Routes.ADD_TO_BASKET, storage)
        {
            m_basketService = basketService;
        }

        public override void Handle(WebRequest webRequest)
        {
            if (Route.Matches(webRequest))
            {
                m_basketService.AddBasketItem(new AddBasketItemRequest
                {
                    BuyerId = SessionCache.LognID,
                    SellerId = webRequest.QueryArguments.ExtractFor<Guid>("seller"),
                    ProductId = webRequest.QueryArguments.ExtractFor<Guid>("product"),
                    Qty = webRequest.QueryArguments.ExtractFor<Int32>("qty")
                });
                Storage.Add(HttpStorageKeys.Json, string.Empty);
            }
            else
            {
                base.NextHandler.Handle(webRequest);
            }
        }
    }
}
